﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace PhotoIDScanner
{

    public partial class frmSave : Form
    {
        MainForm myParent = null;
        String strFilePath = getPath(); //Path.GetTempPath();

        public frmSave(MainForm myParent)
        {
            InitializeComponent();
            this.myParent = myParent;
        }

        private void lblFileName_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
          
            if (!isNumber(txtFileName))
            {
                MessageBox.Show("Please enter a valid Social Security Number.", "Invalid File Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtFileName.Focus();
            }
            else if (txtFileName.Text.Length != 9)
            {
                MessageBox.Show("Please enter a 9 digit Social Security Number.", "Invalid File Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtFileName.Focus();
            }
            else
            {
                String strFileName = strFilePath + txtFileName.Text + ".jpg";
                char chrRandomLetter;

                //If default save location does not exist save ID to user Desktop;
                if (!Directory.Exists(strFilePath))
                {
                    strFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
                    strFileName = strFilePath + txtFileName.Text+ ".jpg";
                    if (File.Exists(strFileName))
                    {
                        chrRandomLetter = RandomLetter.GetLetter();
                        while (File.Exists(strFileName))
                        {
                            strFileName = strFilePath + txtFileName.Text + chrRandomLetter + ".jpg";
                            chrRandomLetter = RandomLetter.GetLetter();
                        }

                        myParent.pictureBoxScannedImage.Image.Save(strFileName, ImageFormat.Jpeg);
                        MessageBox.Show("The Photo ID   " + txtFileName.Text + chrRandomLetter + ".jpg   has been saved to your Desktop.\n\nPlease remember to contact IT Support for help.", "Contact IT Support", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        myParent.savedIDs = myParent.savedIDs + 1;
                    }
                    else
                    {
                        myParent.pictureBoxScannedImage.Image.Save(strFileName, ImageFormat.Jpeg);
                        MessageBox.Show("The PhotoID   " + txtFileName.Text + ".jpg   has been saved to your Desktop.\n\nPlease remember to contact IT Support for help.", "Contact IT Support", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        myParent.savedIDs = myParent.savedIDs + 1;
                    }

                   
                    this.Close();
                  
                    addSSWaterMark(strFilePath + txtFileName.Text + ".jpg", txtFileName.Text);
                      


                }
                else
                {
                    if (File.Exists(strFileName))
                    {
                        chrRandomLetter = RandomLetter.GetLetter();
                        while (File.Exists(strFileName))
                        {
                            strFileName = strFilePath + txtFileName.Text + chrRandomLetter + ".jpg";
                            chrRandomLetter = RandomLetter.GetLetter();

                        }

                        myParent.pictureBoxScannedImage.Image.Save(strFileName, ImageFormat.Jpeg);
                        //MessageBox.Show("A file named   " + txtFileName.Text + ".jpg   already exists." + "\nThe Photo ID has been saved as   " + txtFileName.Text + chrRandomLetter + ".jpg", "ID Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        MessageBox.Show("The Photo ID has been saved.", "ID Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        myParent.savedIDs = myParent.savedIDs + 1;
                       //addSSWaterMark(strFileName, txtFileName.Text);
                        this.Close();
                    }
                    else
                    {
                        myParent.pictureBoxScannedImage.Image.Save(strFileName, ImageFormat.Jpeg);
                        //MessageBox.Show("This Photo ID   " + txtFileName.Text + ".jpg   has been saved", "ID Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        MessageBox.Show("The Photo ID has been saved.", "ID Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        myParent.savedIDs = myParent.savedIDs + 1;
                       // addSSWaterMark(strFileName, txtFileName.Text);
                        this.Close();
                    }
                }
            }
        }

        public static Boolean isNumber(TextBox txtBox)
        {
            string ssNumber = txtBox.Text.Trim();

            //return false if string contains alpha char
            for (int i = 0; i < ssNumber.Length; i++)
            {
                if (!char.IsNumber(ssNumber[i]))
                {
                    return false;
                }

            }

            return true;

        }

        public static void addSSWaterMark(String sFullPath, String sFileName)
        {
            var myBitmap = new Bitmap(sFullPath);
            var g = Graphics.FromImage(myBitmap);
            string ssNumber = sFileName;
            g.DrawString(sFileName.Substring(0, 9), new Font("Tahoma", 10), Brushes.Brown, new PointF(0, 0));
           MessageBox.Show(sFullPath);
            myBitmap.Save(sFullPath , System.Drawing.Imaging.ImageFormat.Jpeg);

        }

        public static String getPath()
        {
            if (File.Exists("settings.xml"))
            {
                XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
                xmlDoc.Load("settings.xml"); //* load the XML document from the specified file.

                //* Get elements.
                XmlNodeList uncPath = xmlDoc.GetElementsByTagName("sharePath");

                //* Display the results.
                return uncPath[0].InnerText;
            }
            else
            {
                MessageBox.Show("I'm lost! For now I'll save this ID to your Desktop.\n\nYou can continue but contact IT Support when finished.", "Contact IT Support", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return @"\\dumb\";
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void frmSave_Load(object sender, EventArgs e)
        {
            txtFileName.Focus();
        }

        private void frmSave_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }
}
